/*
	This file is part of GTimer.

    GTimer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License\.

    GTimer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GTimer. If not, see <http://www.gnu.org/licenses/>.
	
	Copyright 2012 Muhammad Ghareeb Sideek
	Contact: MGhareeb64@gmail.com
 */

package com.gsourcecode.timer;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.gsourcecode.command.Command;
import com.gsourcecode.utils.Colors;
import com.gsourcecode.utils.GInputFilter;

public class NewEventDialog extends Dialog {

	private EditGTimer adpt;
	private Command com;

	public NewEventDialog(Context context, Command inCom) {
		super(context);
		this.adpt = (EditGTimer) context;
		setCancelable(true);
		setTitle("New event");
		setContentView(R.layout.new_event);
		((EditText) findViewById(R.id.hrs)).setFilters(new InputFilter[] { new GInputFilter(4) });
		((EditText) findViewById(R.id.min)).setFilters(new InputFilter[] { new GInputFilter(2) });
		((EditText) findViewById(R.id.sec)).setFilters(new InputFilter[] { new GInputFilter(2) });
		((EditText) findViewById(R.id.col)).setFilters(new InputFilter[] { new GInputFilter("([A-Fa-f0-9])") });
		((EditText) findViewById(R.id.col)).addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

			@Override
			public void afterTextChanged(Editable s) {
				int c = 0;
				try {
					c = Color.parseColor("#" + s);
					((TextView) findViewById(R.id.colV)).setText("#" + s);
				} catch (Exception e) {
					((TextView) findViewById(R.id.colV)).setText("#RRGGBB wrong input");
				}
				((TextView) findViewById(R.id.colV)).setBackgroundColor(c);
			}
		});
		((Button) findViewById(R.id.btnNewEvent)).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String dur = ((EditText) findViewById(R.id.hrs)).getText().toString() + " ";
				dur += ((EditText) findViewById(R.id.min)).getText().toString() + " ";
				dur += ((EditText) findViewById(R.id.sec)).getText().toString();
				String col = "#" + ((EditText) findViewById(R.id.col)).getText().toString();
				String txt = ((EditText) findViewById(R.id.title)).getText().toString();
				if (com == null)
					adpt.add(new Command(dur + " " + col + " \"" + txt + "\""));
				else
					adpt.place(new Command(dur + " " + col + " \"" + txt + "\""));
				cancel();
			}
		});
		setCommand(inCom);
		if (com != null)
			((Button) findViewById(R.id.btnNewEvent)).setText("Update");
	}

	private void setCommand(Command inCom) {
		if (inCom != null) {
			((EditText) findViewById(R.id.hrs)).setText((inCom.time / 1000 / 60 / 60) + "");
			((EditText) findViewById(R.id.min)).setText((inCom.time / 1000 / 60) % 60 + "");
			((EditText) findViewById(R.id.sec)).setText((inCom.time / 1000) % 60 + "");
			((EditText) findViewById(R.id.col)).setText(Colors.toString(inCom.color));
			((EditText) findViewById(R.id.title)).setText(inCom.title + "_" + inCom.sub);
		}
		this.com = inCom;
	}

}
